﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace App
{

	/// <summary>
	/// Defines the default action of a TabToolStripMenuItem.
	/// </summary>
    public enum TabDefaultAction
    {
		/// <summary>
		/// When used, the TabToolStripMenuItem will simply close its tab.
		/// </summary>
        Close = 0,

		/// <summary>
		/// When used, the TabToolStripMenuItem will close all tabs but itself.
		/// </summary>
        CloseAllButThis = 1,

		/// <summary>
		/// When used, the TabToolStripMenuItem will close all tabs.
		/// </summary>
        CloseAll = 2
    }

    public class TabToolStripMenuItem 
        : ToolStripMenuItem
    {
        #region Members
        // Basic members.
        private TabStrip tabParent = null;
        private TabDefaultAction defaultAction;

        private bool useDefaultAction = false;
        #endregion

        #region Properties
        public TabStrip TabParent
        {
            get { return tabParent; }
            set { tabParent = value; }
        }

        public TabDefaultAction DefaultAction
        {
            get { return defaultAction; }
            set { defaultAction = value; }
        }

        [DefaultValue(true)]
        public bool UseDefaultAction
        {
            get { return useDefaultAction; }
            set { useDefaultAction = value; }
        }
        #endregion

        #region Methods
        #region Overrides

        protected override void OnClick(EventArgs e)
        {
            if (useDefaultAction == true)
            {
                if (defaultAction == TabDefaultAction.Close)
                    tabParent.RemoveTab(tabParent.SelectedTab);
                else if (defaultAction == TabDefaultAction.CloseAllButThis)
                {
                    TabStripItem selectedItem = tabParent.SelectedTab;
                    TabStripItemCollection items = new TabStripItemCollection();
                    foreach (TabStripItem item in tabParent.Items)
                    {
                        if (item != selectedItem)
                            items.Add(item);
                    }

                    int tabCount = 0;
                    while (tabCount < items.Count)
                    {
                        tabParent.RemoveTab(items[tabCount]);
                        tabCount++;
                    }
                }
                else if (defaultAction == TabDefaultAction.CloseAll)
                {
                    TabStripItemCollection items = new TabStripItemCollection();
                    foreach (TabStripItem item in tabParent.Items)
                        items.Add(item);

                    int tabCount = 0;
                    while (tabCount < items.Count)
                    {
                        tabParent.RemoveTab(items[tabCount]);
                        tabCount++;
                    }
                }
            }

            base.OnClick(e);
        }
        #endregion

        #endregion

        /// <summary>
        /// Initializes the TabToolStripMenuItem.
        /// </summary>
        public TabToolStripMenuItem(TabStrip parent)
        {
            if (parent != null)
                tabParent = parent;
            else
                throw new ArgumentNullException("parent");
        }
    }
}
